﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using OpenExpressApp.Editors;
using OpenExpressApp.Types;
using OpenExpressApp.Module;
using OpenExpressApp.MetaModel;
using OpenExpressApp.Module.WPF.Editors;

namespace OpenExpressApp.WPF.Editors
{
    /// <summary>
    /// 属性编辑器工厂
    /// </summary>
    public static class PropertyEditorsFactory
    {
        private static Dictionary<string, Type> propertyEditorMap = new Dictionary<string, Type>() { 
            { EditorNames.String, typeof(StringPropertyEditor) },
            { EditorNames.Enum, typeof(EnumPropertyEditor) },
            { EditorNames.LookupDropDown, typeof(LookupListPropertyEditor)},
            { EditorNames.Memo, typeof(MemoPropertyEditor)},
            { EditorNames.Date, typeof(DatePropertyEditor)},
            { EditorNames.FileData, typeof(FileDataPropertyEditor)},
        };

        public static WPFPropertyEditor CreateDefaultEditor(string editorName, IObjectView view, BusinessObjectPropertyInfo propertyInfo)
        {
            var editorType = GetPropertyEditorType(editorName);
            WPFPropertyEditor editor = Activator.CreateInstance(editorType, propertyInfo, view) as WPFPropertyEditor;
            return editor;
        }

        public static void AddEditor(string name, Type type)
        {
            propertyEditorMap.Add(name, type);
        }

        private static Type GetDefaultPropertyEditorType()
        {
            return propertyEditorMap[EditorNames.String];
        }
        private static Type GetPropertyEditorType(string editorName)
        {
            Type editor = null;
            propertyEditorMap.TryGetValue(editorName, out editor);
            return editor ?? GetDefaultPropertyEditorType();
        }
    }
}